package servlets;

import javaBeans.User;
import java.io.IOException;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class AuthenticationServlet extends HttpServlet {
    private static final long serialVersionUID = -8608034654794572382L;
    private HashMap<String, String> _users;


    public void init() {
        _users = new HashMap<String, String>();
	_users.put("nuno", "khan");_users.put("gilmeyer", "rita");_users.put("vasco", "zorrinho");        
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                // Check for request source
        String request_source = request.getParameter("request_source");
        
        if(request_source == null){
            response.setStatus(response.SC_UNAUTHORIZED);
            return;
        }

        else if(request_source.equals("mobile"))
            authenticateUser(request,response);    

        // Source not recognized
        else{
            response.setStatus(response.SC_UNAUTHORIZED);
            return;
        }
        
    }
      private void authenticateUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        try {
            //System.out.println("Authentication Request. USERNAME = " + username + ", PASSWORD = " + password);

            if(username != null && password != null) {
                String verifiedPass;
                if ((verifiedPass = (String) _users.get(username)) != null && verifiedPass.equals(password)) {
                    HttpSession session = request.getSession(true);
                    User userData = new User();
                    userData.setUserName(username);
                    session.setAttribute("user", userData);
                    System.out.println("Authentication Sucsssfull");
                    response.setStatus(response.SC_OK);
 
                }
                else {
                    System.out.println("Problem with authentication");
                    response.setStatus(response.SC_UNAUTHORIZED);                      
                }   
            }

            return;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            response.setStatus(response.SC_UNAUTHORIZED);
            return;
        }
    }

     
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {	       
        processRequest(request, response);
       
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
	doGet(request, response);
    }

}